﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Net;
using Imagine;
using Box2D.XNA;

namespace Carmack
{
    public class CRemoteHero : CActor
    {
        public CRemoteHero(string texture) : base(texture)
        {}

        public void Init( string gamerTag_ )
        {
            m_inputPacket = 0;
            GamerTag = gamerTag_;         //To be used for Tagging
            ActorPhysics.Position = new Vector2(Engine.Instance().ScreenWidth/2,Engine.Instance().ScreenHeight/2);
            ActorPhysics.Friction = 0.0f;
            ActorPhysics.BodyType = BodyType.Dynamic;
            ActorPhysics.InitPhysics( eShapeType.Shape_Circle, id );
            ActorPhysics.HotSpot = eHotSpot.eCenter;
            ActorPhysics.Body.SetUserData("RemoteHero");
            m_velocity = new Vector2( 5.0f, 5.0f );   
            Flags |= (Byte)eGameObjectType.NetworkObject;
            ActorPhysics.HotSpot = eHotSpot.eCenter;

            CWeaponParams prm = new CWeaponParams(1.6f);
            weaponSystem = new CWeaponSystem();
            weaponSystem.InitWeapon(prm, eWeaponClass.Primary, "primary");
            prm.m_weaponClass = eWeaponClass.Secondary;
            prm.m_timer = new CGameTimer(0.75, 0.75);
            prm.m_rateOfFire = 0.6f;
            weaponSystem.InitWeapon(prm, eWeaponClass.Secondary, "Secondary");
            weaponSystem.SetCurrentWeapon(eWeaponClass.Primary);
            weaponSystem.m_primaryWeapon.weaponSprite.Position = ActorPhysics.Position;
            weaponSystem.m_primaryWeapon.weaponSprite.HotSpot = eHotSpot.eBottomCenter;
            weaponSystem.m_secondaryWeapon.weaponSprite.Position = ActorPhysics.Position;
            weaponSystem.m_secondaryWeapon.weaponSprite.HotSpot = eHotSpot.eBottomCenter;
            weaponSystem.GetCurrentWeapon(out m_currentWeapon);
        }

        public override bool Update(GameTime time)
        {
            Vector2 velocity = ActorPhysics.Body.GetLinearVelocity();
            

            if ( (m_inputPacket & (short)eGameKeys.LeftKey) == (short)eGameKeys.LeftKey )
               velocity.X = -m_velocity.X;                                         


            if ( (m_inputPacket & (short)eGameKeys.RightKey) == (short)eGameKeys.RightKey )
                velocity.X = m_velocity.X;               

            if ((m_inputPacket & (short)eGameKeys.ForwardKey) == (short)eGameKeys.ForwardKey )
                velocity.Y = -m_velocity.Y;                            

            if ((m_inputPacket & (short)eGameKeys.BackwardKey) == (short)eGameKeys.BackwardKey )
                velocity.Y = m_velocity.Y;

            if ( (m_inputPacket & (short)eGameKeys.ReloadKey) == (short)eGameKeys.ReloadKey )
                m_currentWeapon.Reload();            

            if ( (m_inputPacket & (short)eGameKeys.FireKey) == (short)eGameKeys.FireKey )
                if (weaponSystem.Fire())
                {
                    GameManager.Instance().SetCameraEffect(eCameraEffect.SmallShake);
                }            

            if ((m_inputPacket & (short)eGameKeys.SwitchWeaponKey) == (short)eGameKeys.SwitchWeaponKey)
            {
                weaponSystem.SwitchPrimarySecondary();
                weaponSystem.GetCurrentWeapon(out m_currentWeapon);
                m_inputPacket &= ~(short)eGameKeys.SwitchWeaponKey;
            }

            if (m_inputPacket == 0)
                velocity = Vector2.Zero;
            
            ActorPhysics.Body.SetLinearVelocity(velocity);                                    

            base.Update(time);        
            weaponSystem.Update(time);
            TransformWeapon();
            
            return true;
        }

        public override bool Draw(GameTime time)

        {  
            ActorPhysics.Angle = m_rotationAngle;
            base.Draw(time);
            weaponSystem.Draw(time);
            return true;
        }      

		public string GamerTag;

        public void DecodePacket( PacketReader reader_ )
        {
            m_inputPacket = reader_.ReadInt32();
            m_rotationAngle = reader_.ReadSingle();                
        }
        
        #region Properties
        public int InputPacket
        {
            set { m_inputPacket = value;    }
        }
        #endregion

        #region Private Parts

        void TransformWeapon()
        {
            m_currentWeapon.weaponSprite.Position = ActorPhysics.Position;
            m_currentWeapon.weaponSprite.Angle = m_rotationAngle;
        }

        int m_inputPacket;
        CWeaponSystem weaponSystem;
        CWeapon m_currentWeapon;
        #endregion
    }
}
